package com.tsmti.activiti.service.impl;

import com.tsmti.core.common.annotation.ActField;
import com.tsmti.activiti.entity.ExtendActBusiness;
import com.tsmti.activiti.entity.ExtendActModel;
import com.tsmti.activiti.service.ExtendActBusinessService;
import com.tsmti.activiti.utils.ActConstants;
import com.tsmti.activiti.utils.AnnotationUtils;
import com.tsmti.core.common.service.impl.BaseServiceImpl;
import com.tsmti.core.exception.MyException;
import com.tsmti.core.util.StringUtils;
import com.tsmti.system.entity.User;
import com.tsmti.util.UserUtils;
import org.hibernate.criterion.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Package：com.tsmti.activiti.service.impl
 * @Class：ExtendActBusinessServiceImpl
 * @Description： TODO
 * @Author：zcw
 * @Date：Created in 2018/8/16 15:10
 * @Company:
 * @Version：
 * @Modified By:
 *
 */
@Service("extendActBusinessService")
@Transactional(rollbackFor = Exception.class)
public class ExtendActBusinessServiceImpl extends BaseServiceImpl<ExtendActBusiness> implements ExtendActBusinessService {


	@Override
	public List<ExtendActBusiness> queryListByBean(ExtendActBusiness extendActBusiness) {

        DetachedCriteria busCriteria = DetachedCriteria.forClass(ExtendActBusiness.class);
        if(!StringUtils.isEmpty(extendActBusiness.getName())){
            busCriteria.add(Restrictions.like("name",extendActBusiness.getName()));
        }
        if(!StringUtils.isEmpty(extendActBusiness.getType())){
            busCriteria.add(Restrictions.eq("name",extendActBusiness.getType()));
        }
        List<ExtendActBusiness> busList = list(busCriteria);
        User currentUser = UserUtils.getUser();
        if(busList ==null || busList.size()<1){
            ExtendActBusiness bus = new ExtendActBusiness();
            bus.setId(StringUtils.randomUUID());
            bus.setName("业务树根目录");
            bus.setOpen("true");
            bus.setActKey("root");
            bus.setParentId(bus.getId());
            bus.setSort("1");
            bus.setType(ActConstants.ActBusType.ROOT.getValue());
            bus.setCreateDate(new Date());
            bus.setOrgId(currentUser.getOrgId());
            bus.setCreateUser(StringUtils.null2String(currentUser.getId()));
            bus.setRemark("业务树初始化");
            save(bus);
            busList.add(bus);
        }
        return busList;
	}

    @Override
    public void edit(ExtendActBusiness bus) {
        //根据流程key查询
        ExtendActBusiness businessEntity = get("actKey",bus.getActKey());
        if(businessEntity != null && !businessEntity.getId().equals(bus.getId())){
            throw new MyException("流程key已经存在,请重复输入");
        }
        if(StringUtils.isEmpty(bus.getId())){
            //保存
            bus.setId(StringUtils.randomUUID());
        }
        save(bus);
    }

    @Override
    public List<ExtendActBusiness> queryBusTree() {
        DetachedCriteria busCriteria = DetachedCriteria.forClass(ExtendActBusiness.class);
        Disjunction dis=Restrictions.disjunction();
        dis.add(Restrictions.eq("type", ActConstants.ActBusType.GROUP.getValue()));
        dis.add(Restrictions.eq("type", ActConstants.ActBusType.BUS.getValue()));
        busCriteria.add(dis);
        return list(busCriteria);
    }

    @Override
    public ExtendActBusiness queryActBusByModelId(String modelId) {
        DetachedCriteria busCriteria = DetachedCriteria.forClass(ExtendActBusiness.class);
        DetachedCriteria modelCriteria = DetachedCriteria.forClass(ExtendActModel.class);
        modelCriteria.setProjection(Property.forName("extendActBusinessId"));
        modelCriteria.add(Restrictions.eq("modelId", modelId));
        busCriteria.add(Property.forName("id").in(modelCriteria));
        ExtendActBusiness businessEntity = null;
        List<ExtendActBusiness> actBusinessList = list(busCriteria);
        if(actBusinessList != null && actBusinessList.size()>0){
            businessEntity = actBusinessList.get(0);
        }
        //业务实体类
        String classurl = businessEntity.getClassUrl();
        //可写
//        List<Map<String,Object>> writes=new ArrayList<Map<String,Object>>();
        //可设置为条件
//        List<Map<String,Object>> judgs=new ArrayList<>();
        Map<String,String> writes = new HashMap(16);
        Map<String,String> judgs = new HashMap(16);
        Map<String,String> userFields = new HashMap(16);
        judgs.put("isAgree", "是否通过");
        List<Map<String, Object>> mapList = AnnotationUtils.getActFieldByClazz(classurl);
        for (Map remap:mapList){
//            writes = new HashMap(16);
            ActField actField= (ActField) remap.get("actField");
            String keyName = (String) remap.get("keyName");
            if(actField!=null){
                writes.put(keyName,  actField.name());
                if(actField.isJudg()){
                    judgs.put(keyName, actField.name());
                }
                if(actField.isUser()){
                    userFields.put(keyName,actField.name());
                }
            }
        }
        businessEntity.setJudgs(judgs);
        businessEntity.setWrites(writes);
        businessEntity.setUserFields(userFields);
        return businessEntity;
    }

    @Override
    public List<ExtendActBusiness> queryCalBackByPid(String parentId) {
        List<ExtendActBusiness>  actBusinessList =  list("parentId",parentId);
        return actBusinessList;
    }

    @Override
    public ExtendActBusiness queryByActKey(String actKey) {
        return get("actKey",actKey);
    }
}
